# contextual menus
# (c) 2008-2009 NIXON Development Corporation.

import wx
import os, thread
import format, local, common
import prefManager
from ftplib import error_temp, error_perm, error_proto
from alert import alertBox

class contextMenu(wx.Menu):
	def __init__(self, object, hdlFrame, blLocal):
		wx.Menu.__init__(self)

		self.object = object
		self.hdlFrame = hdlFrame
		self.blLocal = blLocal
		
		self.sel = object.getSelections()
		self.lbl = object.Label
		self.ls = len(self.sel)
		
		if self.ls == 1:
			self.txt = self.object.GetItemText(self.sel[0])
			if self.blLocal:
				self.type = local.getType(self.lbl + '/' + self.txt)
			else:
				self.type = self.hdlFrame.dirList.getType(self.lbl + '/' + self.txt)

			mnuOpen = self.Append(-1, 'Open %s' % self.txt)
			self.Bind(wx.EVT_MENU, self.onOpen, mnuOpen)
			self.AppendSeparator()
		
		mnuGetInfo = self.Append(-1, '&Get Info')
		self.Bind(wx.EVT_MENU, self.onGetInfo, mnuGetInfo)
			
		if self.ls > 0:
			if self.ls > 1:
				strItems = ' these %d items' % self.ls
			elif self.ls == 1:
				name = self.txt
				self.realName = format.getRealName(hdlFrame, self.lbl, name, blLocal)
				strItems = ' "%s"' % self.realName
				
				mnuRename = self.Append(-1, '&Rename' + strItems)
				self.Bind(wx.EVT_MENU, self.onRename, mnuRename)
				
			mnuDelete2 = wx.Menu()
			mnuDelete = mnuDelete2.Append(-1, '&Delete')
			mnuDelete1 = self.AppendMenu(-1, '&Delete' + strItems, mnuDelete2)
			
			self.Bind(wx.EVT_MENU, self.onDelete, mnuDelete)
		else:
			strItems = ' "%s"' % self.lbl
			
		mnuCreateDir = self.Append(-1, '&New Folder')
		self.Bind(wx.EVT_MENU, self.onCreateDir, mnuCreateDir)

		mnuSelectAll = self.Append(-1, 'Select &All')
		self.Bind(wx.EVT_MENU, object.selectAll, mnuSelectAll)

		if self.ls > 0:
			self.AppendSeparator()
			
			if blLocal: strTransfer = '&Upload'
			else: strTransfer = '&Download'
			
			mnuTransfer = self.Append(-1, strTransfer + strItems)
			self.Bind(wx.EVT_MENU, self.onTransfer, mnuTransfer)
		
	def onDelete(self, event):
		if not self.blLocal: self.hdlFrame.mFTP.chgDir(self.lbl)
		
		self.hdlFrame.mFTP.tl.addTask(self.delete, (), self.deleteConsumer)
		
	def delete(self):
		for x in range(len(self.sel)):
			item = self.sel[0]
			name = self.object.GetItemText(item)
			realName = format.getRealName(self.hdlFrame, self.lbl, name, self.blLocal)
			
			pwf = self.lbl + '/' + realName
			
			if self.blLocal:
				finfo = local.getType(pwf)
				if finfo == 2:
					local.deleteTree(pwf, self.hdlFrame, self.object)
				else:
					os.remove(pwf)
			else:
				finfo = self.hdlFrame.dirList.getInfo(pwf)[0]
				finfo = finfo[:1]
				if finfo == 'd':
					self.hdlFrame.mFTP.deleteTree(pwf, self.object)
				else:
					self.hdlFrame.mFTP.hftp.delete(name)
			
			self.object.DeleteItem(item)
			self.sel = self.object.getSelections()
			
	def deleteConsumer(self, delayedresult):
		try:
			r = delayedresult.get()
			#self.hdlFrame.reloadRemote(self.object.Number - 1)
		except error_perm:
			if self.lbl == '': lbl = '/'
			else: lbl = self.lbl
			dlg = alertBox(self.hdlFrame, \
				'You do not have permission to delete files or folders from the folder "' + lbl + self.object.GetItemText(self.sel[0]) + '"', \
				'Make sure that you have the appropriate access privileges.')
		
	def onOpen(self, event):
		if self.type == 2 or self.type == 'd':
			if self.blLocal:
				panel = self.hdlFrame.pnLocal
				func = self.hdlFrame.UpdateLocal
			else:
				panel = self.hdlFrame.pnRemote
				func = self.hdlFrame.UpdateList
				
			panel.removeListBox(self.object.Number)
			panel.addListBox(self.txt)
						
			pwd = self.object.Label + '/' + self.txt
			func(pwd)
		else:
			self.hdlFrame.preview.windowPreview(self.object.Label + '/' + self.txt, self.blLocal)
		
	def onGetInfo(self, event):
		infoDlg = common.infoDialog(self.hdlFrame)
		if self.ls == 1:
			sel = self.txt
			infoDlg.showInfo(common.getInfo(self.hdlFrame, self.blLocal, self.lbl + '/' + sel, sel), self.blLocal)
		infoDlg.Show()
		self.hdlFrame.infoDlg = infoDlg

	def onCreateDir(self, event):
		response = ''
		dlg = wx.TextEntryDialog(self.hdlFrame, "Enter the name of the directory:",
				'Create Directory', 'untitled folder')
		if dlg.ShowModal() == wx.ID_OK:
			response = dlg.GetValue()
		dlg.Destroy()
		
		if response != '':
			if self.type == 2 or self.type == 'd':
				dir = self.txt + '/' + response
			else:
				dir = response
				
			pwd = self.lbl + '/' + dir
					
			if self.blLocal:				
				os.mkdir(pwd)
			else:
				self.hdlFrame.mFTP.createDir(pwd, self.object)
				
	def reload(self):
		num = self.object.Number
		
		if self.blLocal:
			self.hdlFrame.pnLocal.removeListBox(num)
			self.hdlFrame.localbox = self.object
			self.hdlFrame.UpdateLocal(self.lbl)
		else:
			self.hdlFrame.pnRemote.removeListBox(num)
			self.hdlFrame.box = self.object
			self.hdlFrame.UpdateList(self.lbl)

	def onRename(self, event):
		response = ''
		
		dlg = wx.TextEntryDialog(self.hdlFrame, 'Enter the new name for "%s"' % self.realName,
				'Rename', self.realName)
		if dlg.ShowModal() == wx.ID_OK:
			response = dlg.GetValue()
			dlg.Destroy()
			
		if response != '':
			if self.blLocal:
				strBefore = self.lbl + '/'
				os.rename(strBefore + self.realName, strBefore + response)
			else:
				self.hdlFrame.mFTP.renameFile(self.lbl, self.realName, response, self.object.Number - 1)
				
	def onTransfer(self, event):
		lstFiles = []
		
		for x in self.sel:
			name = self.object.GetItemText(x)
			lstFiles.append(name)
			
		if self.blLocal:
			box = self.hdlFrame.pnRemote.lstBrowsers[-1]
		else:
			box = self.hdlFrame.pnLocal.lstBrowsers[-1]
			
		self.hdlFrame.mFTP.transfer(self.lbl, lstFiles, self.hdlFrame, self.blLocal, box)
		
class menuBar(wx.MenuBar):
	def __init__(self, hdlFrame):
		wx.MenuBar.__init__(self)
		self.hdlFrame = hdlFrame
		
		mnuRemote = wx.Menu()
		mnuRemote.Append(-1, '&Connect...\tCtrl-Space')
		self.Append(mnuRemote, '&Remote')
		
		mnuLocal = wx.Menu()
		mnuLocal.Append(-1, '&Upload')
		self.Append(mnuLocal, '&Local')
		
		mnuGo = wx.Menu()
		mnuGo.Append(-1, '&Go to Path...')
		self.Append(mnuGo, '&Go')
		
		self.mnuType = wx.Menu()
		self.mnuTypeAutomatic = self.mnuType.AppendCheckItem(-1, 'Au&tomatic')
		self.mnuTypeAutomatic.Check(True)
		self.hdlFrame.Bind(wx.EVT_MENU, self.onTypeAuto, self.mnuTypeAutomatic)
		
		self.mnuType.AppendSeparator()
		
		self.mnuType.Append(254, '&ASCII', kind=wx.ITEM_CHECK)
		self.mnuType.Append(255, '&Binary', kind=wx.ITEM_CHECK)
		self.hdlFrame.Bind(wx.EVT_MENU_RANGE, self.onTypeTransfer, id=254, id2=255)
		self.Append(self.mnuType, '&Type')
		
		self.mnuType.Check(255, True)
		
		mnuTools = wx.Menu()
		mnuTools.Append(-1, '&Queue')
		mnuTools.Append(-1, '&Filter...')
		mnuTools.AppendSeparator()
		
		mnuToolsPreferences = mnuTools.Append(-1, '&Preferences...')
		self.hdlFrame.Bind(wx.EVT_MENU, self.onToolsPreferences, mnuToolsPreferences)
		
		self.Append(mnuTools, '&Tools')
		
		mnuHelp = wx.Menu()
		mnuHelp.Append(-1, '&About NIXON Shuttle...')
		self.Append(mnuHelp, '&Help')
		
	def onTypeAuto(self, event):
		self.hdlFrame.blAuto = event.IsChecked()
		
	def onTypeTransfer(self, event):
		self.Check(254, False)
		self.Check(255, False)
		self.Check(event.GetId(), True)
		self.mnuTypeAutomatic.Check(False)
		self.hdlFrame.blAuto = False
		#event.Skip()
		
	def onToolsPreferences(self, event):
		pw = prefManager.prefWindow(self.hdlFrame)
		pw.ShowModal()
